package models

import (
	"activity_srv/basic/config"
	"gorm.io/gorm"
	"time"
)

// 发票表
type Invoice struct {
	Id          uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	Title       string         `gorm:"column:title;type:varchar(255);comment:发票抬头;default:NULL;" json:"title"`            // 发票抬头
	UserId      int32          `gorm:"column:user_id;type:int;comment:用户id;default:NULL;" json:"user_id"`                 // 用户id
	OrderId     int32          `gorm:"column:order_id;type:int;comment:订单id;default:NULL;" json:"order_id"`               // 订单id
	InvoicesNo  string         `gorm:"column:invoices_no;type:varchar(50);comment:发票编号;default:NULL;" json:"invoices_no"` // 发票编号
	BankName    string         `gorm:"column:bank_name;type:varchar(255);comment:银行名称;default:NULL;" json:"bank_name"`    // 银行名称
	Address     string         `gorm:"column:address;type:varchar(255);comment:企业地址;default:NULL;" json:"address"`        // 企业地址
	InvoiceType int32          `gorm:"column:invoice_type;type:int;comment:类型;default:NULL;" json:"invoice_type"`         // 类型
	Amount      float64        `gorm:"column:amount;type:decimal(10, 2);comment:发票金额;default:NULL;" json:"amount"`        // 发票金额
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (i *Invoice) TableName() string {
	return "invoice"
}

// 添加发票
func (i *Invoice) CreateInvoice() error {
	return config.DB.Create(i).Error
}

// 发票展示
func (i *Invoice) ListInvoice() (list []*Invoice, err error) {
	err = config.DB.Where("id = ?", i.Id).Find(&list).Error
	return
}

// 删除发票
func (i *Invoice) DelInvoice() error {
	return config.DB.Delete(i).Error
}

func (i *Invoice) GetInvoiceId() error {
	return config.DB.Where("id = ?", i.Id).First(i).Error
}
